from appium_practice.PO_MODE.page.basepage import CommonBasePage


class FindMembersPage(CommonBasePage):
    def goto_main_address_page(self):
        from appium_practice.PO_MODE.page.addresspage import AddressPage
        self.find_app_element_and_click(('XPATH',"//android.widget.EditText[@text='搜索']/../preceding-sibling::android"
                                                 ".widget.LinearLayout[1]/child::android.widget.TextView"))
        return AddressPage(self.appium_driver)

    def get_member_element(self, name):

        self.find_app_element_and_input(('XPATH', "//android.widget.EditText"), name)
        element = self.find_app_element_and_get_attr(('XPATH', f"//android.widget.TextView[contains(@text,'{name}')]"))
        self.find_app_element_and_click(('XPATH', f"//android.widget.EditText[contains(@text,'{name}')]/"
                                                  f"following-sibling::android.widget.ImageView"))
        return element,FindMembersPage(self.appium_driver)

    def delete_member(self, name):
        #实现删除联系人功能 PO封装

        # 输入的需要删除联系人
        self.find_app_element_and_input(('XPATH', "//android.widget.EditText"), name)
        self.wait(2)
        # 点击联系人
        self.find_app_element_and_click(('XPATH', f"//android.widget.TextView[@text='{name}']"))
        # 点击右上角按钮
        self.find_app_element_and_click(
            ('XPATH', "//android.widget.TextView[@text='个人信息']/../../../../following-sibling"
                      "::android.widget.LinearLayout["
                      "1]/child::android.widget.RelativeLayout/android.widget.TextView"))
        # 点击编辑成员
        self.find_app_element_and_click(('XPATH',"//android.widget.TextView[@text='编辑成员']"))
        # 下滑
        self.swipe_down(num=3)
        # 点击删除
        self.find_app_element_and_click(('XPATH',"//android.widget.TextView[@text='删除成员']"))
        # 确定
        self.find_app_element_and_click(('XPATH',"//android.widget.TextView[@text='确定']"))
        self.wait(2)
        element_text = self.find_app_element_and_get_attr(('XPATH',"//android.widget.TextView[@text='无搜索结果']"))
        # 清除输入
        self.find_app_element_and_click(('XPATH', f"//android.widget.EditText[contains(@text,'{name}')]/"
                                                  f"following-sibling::android.widget.ImageView"))
        return element_text,FindMembersPage(self.appium_driver)

